<template>
  <view class="medicine-records-page">
    <!-- 添加按钮 -->
    <view class="add-btn" @tap="showAddModal">
      <text class="add-icon">+</text>
      <text class="add-text">添加药品记录</text>
    </view>

    <!-- 药品列表 -->
    <view class="records-list">
      <view 
        v-for="record in records" 
        :key="record.id"
        class="record-card"
      >
        <view class="record-header">
          <view class="medicine-icon">💊</view>
          <view class="record-actions">
            <view class="action-btn" @tap="editRecord(record)">
              <text>✏️</text>
            </view>
            <view class="action-btn" @tap="deleteRecord(record.id)">
              <text>🗑️</text>
            </view>
          </view>
        </view>
        
        <view class="record-content">
          <text class="medicine-name">{{ record.name }}</text>
          <view class="medicine-details">
            <view class="detail-item">
              <text class="detail-label">用量：</text>
              <text class="detail-value">{{ record.dosage }}</text>
            </view>
            <view class="detail-item">
              <text class="detail-label">频率：</text>
              <text class="detail-value">{{ record.frequency }}</text>
            </view>
            <view class="detail-item">
              <text class="detail-label">开始日期：</text>
              <text class="detail-value">{{ record.startDate }}</text>
            </view>
            <view v-if="record.endDate" class="detail-item">
              <text class="detail-label">结束日期：</text>
              <text class="detail-value">{{ record.endDate }}</text>
            </view>
            <view v-if="record.notes" class="detail-item">
              <text class="detail-label">备注：</text>
              <text class="detail-value">{{ record.notes }}</text>
            </view>
          </view>
        </view>

        <view v-if="record.reminder" class="reminder-badge">
          <text class="reminder-icon">⏰</text>
          <text class="reminder-text">已设置提醒</text>
        </view>
      </view>

      <view v-if="records.length === 0" class="empty-state">
        <text class="empty-icon">💊</text>
        <text class="empty-text">暂无药品记录</text>
        <text class="empty-hint">点击上方按钮添加药品</text>
      </view>
    </view>

    <!-- 添加/编辑弹窗 -->
    <view v-if="showModal" class="modal-overlay" @tap="closeModal">
      <view class="modal-content" @tap.stop>
        <view class="modal-header">
          <text class="modal-title">{{ isEdit ? '编辑药品' : '添加药品' }}</text>
          <view class="modal-close" @tap="closeModal">
            <text>✕</text>
          </view>
        </view>
        
        <scroll-view class="modal-body" scroll-y>
          <view class="form-group">
            <text class="form-label">药品名称</text>
            <input 
              v-model="formData.name" 
              class="form-input"
              placeholder="请输入药品名称"
            />
          </view>

          <view class="form-group">
            <text class="form-label">用量</text>
            <input 
              v-model="formData.dosage" 
              class="form-input"
              placeholder="例如：250mg"
            />
          </view>

          <view class="form-group">
            <text class="form-label">服用频率</text>
            <input 
              v-model="formData.frequency" 
              class="form-input"
              placeholder="例如：每日3次"
            />
          </view>

          <view class="form-group">
            <text class="form-label">开始日期</text>
            <picker mode="date" :value="formData.startDate" @change="onStartDateChange">
              <view class="picker-input">
                <text>{{ formData.startDate || '请选择日期' }}</text>
              </view>
            </picker>
          </view>

          <view class="form-group">
            <text class="form-label">结束日期（可选）</text>
            <picker mode="date" :value="formData.endDate" @change="onEndDateChange">
              <view class="picker-input">
                <text>{{ formData.endDate || '请选择日期' }}</text>
              </view>
            </picker>
          </view>

          <!-- 添加图片上传功能 -->
          <view class="form-group">
            <text class="form-label">药品照片（可选）</text>
            <view class="image-upload-section">
              <view 
                v-for="(img, index) in formData.images" 
                :key="index"
                class="image-item"
              >
                <image :src="img" class="uploaded-image" mode="aspectFill" />
                <view class="image-delete" @tap="deleteImage(index)">
                  <text>✕</text>
                </view>
              </view>
              <view v-if="formData.images.length < 3" class="upload-btn-wrapper">
                <view class="upload-btn" @tap="chooseImage">
                  <text class="upload-icon">📷</text>
                  <text class="upload-text">上传照片</text>
                </view>
              </view>
            </view>
            <text class="form-hint">最多上传3张照片</text>
          </view>

          <view class="form-group">
            <text class="form-label">备注</text>
            <textarea 
              v-model="formData.notes" 
              class="form-textarea"
              placeholder="请输入备注信息（可选）"
              maxlength="200"
            />
          </view>

          <view class="form-group">
            <view class="checkbox-item" @tap="toggleReminder">
              <view class="checkbox" :class="{ checked: formData.reminder }">
                <text v-if="formData.reminder">✓</text>
              </view>
              <text class="checkbox-label">设置用药提醒</text>
            </view>
          </view>
        </scroll-view>

        <view class="modal-footer">
          <view class="modal-btn cancel" @tap="closeModal">
            <text class="btn-text">取消</text>
          </view>
          <view class="modal-btn confirm" @tap="saveRecord">
            <text class="btn-text">保存</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

const records = ref([
  {
    id: 1,
    name: '阿莫西林胶囊',
    dosage: '250mg',
    frequency: '每日3次',
    startDate: '2024-01-15',
    endDate: '2024-01-22',
    notes: '饭后服用',
    reminder: true,
    images: []
  },
  {
    id: 2,
    name: '维生素C',
    dosage: '100mg',
    frequency: '每日1次',
    startDate: '2024-01-01',
    endDate: '',
    notes: '长期服用',
    reminder: false,
    images: []
  }
])

const showModal = ref(false)
const isEdit = ref(false)
const currentId = ref(null)

const formData = ref({
  name: '',
  dosage: '',
  frequency: '',
  startDate: '',
  endDate: '',
  notes: '',
  reminder: false,
  images: []
})

const showAddModal = () => {
  isEdit.value = false
  formData.value = {
    name: '',
    dosage: '',
    frequency: '',
    startDate: new Date().toISOString().split('T')[0],
    endDate: '',
    notes: '',
    reminder: false,
    images: []
  }
  showModal.value = true
}

const closeModal = () => {
  showModal.value = false
}

const onStartDateChange = (e) => {
  formData.value.startDate = e.detail.value
}

const onEndDateChange = (e) => {
  formData.value.endDate = e.detail.value
}

const toggleReminder = () => {
  formData.value.reminder = !formData.value.reminder
}

const saveRecord = () => {
  if (!formData.value.name || !formData.value.dosage || !formData.value.frequency) {
    wx.showToast({
      title: '请填写必填项',
      icon: 'none'
    })
    return
  }

  if (isEdit.value) {
    const index = records.value.findIndex(r => r.id === currentId.value)
    if (index !== -1) {
      records.value[index] = { ...formData.value, id: currentId.value }
    }
  } else {
    const newRecord = {
      ...formData.value,
      id: Date.now()
    }
    records.value.unshift(newRecord)
  }

  wx.showToast({
    title: '保存成功',
    icon: 'success'
  })

  closeModal()
}

const editRecord = (record) => {
  isEdit.value = true
  currentId.value = record.id
  formData.value = { ...record }
  showModal.value = true
}

const deleteRecord = (id) => {
  wx.showModal({
    title: '确认删除',
    content: '确定要删除这条药品记录吗？',
    success: (res) => {
      if (res.confirm) {
        records.value = records.value.filter(r => r.id !== id)
        wx.showToast({
          title: '删除成功',
          icon: 'success'
        })
      }
    }
  })
}

const chooseImage = () => {
  if (typeof wx !== 'undefined') {
    wx.chooseMedia({
      count: 3 - formData.value.images.length,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFiles = res.tempFiles.map(file => file.tempFilePath)
        formData.value.images = [...formData.value.images, ...tempFiles]
      }
    })
  }
}

const deleteImage = (index) => {
  formData.value.images.splice(index, 1)
}
</script>

<style scoped>
.medicine-records-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 32rpx;
}

.add-btn {
  background: linear-gradient(135deg, #8b5cf6, #ec4899);
  border-radius: 16rpx;
  padding: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16rpx;
  margin-bottom: 32rpx;
  box-shadow: 0 4rpx 12rpx rgba(139, 92, 246, 0.3);
  transition: all 0.2s;
}

.add-btn:active {
  transform: scale(0.98);
}

.add-icon {
  font-size: 40rpx;
  color: white;
  font-weight: bold;
}

.add-text {
  font-size: 28rpx;
  color: white;
  font-weight: 500;
}

.records-list {
  display: flex;
  flex-direction: column;
  gap: 24rpx;
}

.record-card {
  background: white;
  border-radius: 16rpx;
  padding: 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  position: relative;
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24rpx;
}

.medicine-icon {
  font-size: 48rpx;
  width: 80rpx;
  height: 80rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f3e8ff;
  border-radius: 50%;
}

.record-actions {
  display: flex;
  gap: 16rpx;
}

.action-btn {
  width: 56rpx;
  height: 56rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: #f3f4f6;
  font-size: 28rpx;
  transition: background-color 0.2s;
}

.action-btn:active {
  background: #e5e7eb;
}

.record-content {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
}

.medicine-name {
  font-size: 32rpx;
  font-weight: 600;
  color: #1f2937;
  display: block;
}

.medicine-details {
  display: flex;
  flex-direction: column;
  gap: 12rpx;
}

.detail-item {
  display: flex;
  gap: 16rpx;
}

.detail-label {
  font-size: 28rpx;
  color: #6b7280;
  min-width: 140rpx;
}

.detail-value {
  font-size: 28rpx;
  color: #1f2937;
  flex: 1;
}

.reminder-badge {
  display: flex;
  align-items: center;
  gap: 8rpx;
  background: #fef3c7;
  padding: 12rpx 16rpx;
  border-radius: 8rpx;
  margin-top: 16rpx;
  width: fit-content;
}

.reminder-icon {
  font-size: 24rpx;
}

.reminder-text {
  font-size: 24rpx;
  color: #92400e;
}

.empty-state {
  text-align: center;
  padding: 120rpx 32rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16rpx;
}

.empty-icon {
  font-size: 96rpx;
  opacity: 0.3;
}

.empty-text {
  font-size: 32rpx;
  color: #6b7280;
  display: block;
}

.empty-hint {
  font-size: 24rpx;
  color: #9ca3af;
  display: block;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 32rpx;
  box-sizing: border-box;
}

.modal-content {
  background: white;
  border-radius: 24rpx;
  width: 100%;
  max-width: 600rpx;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
}

.modal-header {
  padding: 32rpx;
  border-bottom: 1rpx solid #e5e7eb;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #1f2937;
}

.modal-close {
  width: 48rpx;
  height: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
  font-size: 32rpx;
  color: #6b7280;
}

.modal-close:active {
  background: #f3f4f6;
}

.modal-body {
  flex: 1;
  padding: 32rpx;
  overflow-y: auto;
  box-sizing: border-box;
}

.form-group {
  margin-bottom: 32rpx;
  box-sizing: border-box;
}

.form-label {
  font-size: 28rpx;
  color: #374151;
  display: block;
  margin-bottom: 12rpx;
  font-weight: 500;
}

/* 修复输入框宽度和高度问题 */
.form-input {
  width: 100%;
  height: 80rpx;
  padding: 0 24rpx;
  border: 1rpx solid #d1d5db;
  border-radius: 12rpx;
  font-size: 28rpx;
  background: #f9fafb;
  box-sizing: border-box;
  line-height: 80rpx;
}

.picker-input {
  width: 100%;
  height: 80rpx;
  padding: 0 24rpx;
  border: 1rpx solid #d1d5db;
  border-radius: 12rpx;
  font-size: 28rpx;
  background: #f9fafb;
  box-sizing: border-box;
  display: flex;
  align-items: center;
}

.picker-input text {
  color: #374151;
}

.form-textarea {
  width: 100%;
  padding: 20rpx 24rpx;
  border: 1rpx solid #d1d5db;
  border-radius: 12rpx;
  font-size: 28rpx;
  background: #f9fafb;
  min-height: 120rpx;
  box-sizing: border-box;
  line-height: 1.6;
}

/* 添加图片上传样式 */
.image-upload-section {
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;
}

.image-item {
  width: 160rpx;
  height: 160rpx;
  position: relative;
  border-radius: 12rpx;
  overflow: hidden;
}

.uploaded-image {
  width: 100%;
  height: 100%;
}

.image-delete {
  position: absolute;
  top: 8rpx;
  right: 8rpx;
  width: 40rpx;
  height: 40rpx;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24rpx;
}

.upload-btn-wrapper {
  width: 160rpx;
  height: 160rpx;
}

.upload-btn {
  width: 100%;
  height: 100%;
  border: 2rpx dashed #d1d5db;
  border-radius: 12rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8rpx;
  background: #f9fafb;
  transition: all 0.2s;
}

.upload-btn:active {
  background: #f3f4f6;
  border-color: #9ca3af;
}

.upload-icon {
  font-size: 48rpx;
}

.upload-text {
  font-size: 24rpx;
  color: #6b7280;
}

.form-hint {
  font-size: 24rpx;
  color: #9ca3af;
  display: block;
  margin-top: 8rpx;
}

.modal-footer {
  padding: 32rpx;
  border-top: 1rpx solid #e5e7eb;
  display: flex;
  gap: 16rpx;
}

.modal-btn {
  flex: 1;
  padding: 24rpx;
  border-radius: 12rpx;
  text-align: center;
  transition: all 0.2s;
}

.modal-btn.cancel {
  background: #f3f4f6;
}

.modal-btn.cancel:active {
  background: #e5e7eb;
}

.modal-btn.confirm {
  background: #3b82f6;
}

.modal-btn.confirm:active {
  background: #2563eb;
}

.btn-text {
  font-size: 28rpx;
  font-weight: 500;
  color: #374151;
}

.modal-btn.confirm .btn-text {
  color: white;
}

.checkbox-item {
  display: flex;
  align-items: center;
  gap: 16rpx;
  padding: 16rpx 0;
}

.checkbox {
  width: 40rpx;
  height: 40rpx;
  border: 2rpx solid #d1d5db;
  border-radius: 8rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}

.checkbox.checked {
  background: #3b82f6;
  border-color: #3b82f6;
  color: white;
}

.checkbox-label {
  font-size: 28rpx;
  color: #374151;
}
</style>
